package com.da.javatest.niuke.baodian;

/**
 * @author chenlida
 * @date 2020/10/4 12:23
 * https://www.nowcoder.com/practice/164929d4acd04de5b0ee2d93047b3b20?tpId=8&tqId=10994&rp=1&
 * ru=%2Fta%2Fcracking-the-coding-interview&qru=%2Fta%2Fcracking-the-coding-interview%2Fquestion-ranking
 */
public class C3Same {
    public static void main(String[] args) {
        System.out.println(checkSam("This is nowcoder", "is This nowcoder"));
        System.out.println(checkSam("Here you are", "Are you here"));
    }

    public static boolean checkSam(String stringA, String stringB) {
        if (stringA == null || stringB == null || stringA.length() != stringB.length()) {
            return false;
        }
        // 记录字符
        int[] cnt = new int[256];
        for (int i = 0; i < stringA.length(); i++) {
            cnt[stringA.charAt(i)]++;
        }
        // 判断上面A串的字符和B串的是否存在不同
        for (int i = 0; i < stringB.length(); i++) {
            if (--cnt[stringB.charAt(i)] < 0) {
                return false;
            }
        }
        return true;
    }
}
